//
//  BKZhiYuanViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/1.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKZhiYuanViewController.h"
#import "BKSearchField.h"
#import "BKResultZhiYuanViewController.h"
#import "BKZhiYuanViewModel.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "BKHistoryViewModel.h"

@interface BKZhiYuanViewController ()
@property (weak, nonatomic) IBOutlet BKSearchField *kaofen;
@property (weak, nonatomic) IBOutlet BKSearchField *zhunkaozheng;
@property (weak, nonatomic) IBOutlet BKSearchField *kaoqu;
@property (weak, nonatomic) IBOutlet BKSearchField *zhiyuan1;
@property (weak, nonatomic) IBOutlet BKSearchField *zhiyuan2;
@property (weak, nonatomic) IBOutlet BKSearchField *zhiyuan3;
@property (weak, nonatomic) IBOutlet BKSearchField *zhiyuan4;
@property (weak, nonatomic) IBOutlet BKSearchField *zhiyuan5;
@property (weak, nonatomic) IBOutlet BKSearchField *diqu1;
@property (weak, nonatomic) IBOutlet BKSearchField *diqu2;
@property (weak, nonatomic) IBOutlet BKSearchField *diqu3;
@property (weak, nonatomic) IBOutlet BKSearchField *diqu4;
@property (weak, nonatomic) IBOutlet BKSearchField *diqu5;

@end

@implementation BKZhiYuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    @weakify(self);
    
    [RACObserve(self.diqu1, text) subscribeNext:^(id x) {
        
        @strongify(self);
        
        if ([x isKindOfClass:[NSString class]]&& [x length]>0) {
            [BKHistoryViewModel colleageWithProv:x success:^(NSArray *dataArray) {
                self.zhiyuan1.colleageArray = dataArray;
                self.zhiyuan1.flag = 8004;
                
            } failure:^(NSString *message) {
                
            }];
        }
        
    }];
    
    [RACObserve(self.diqu3, text) subscribeNext:^(id x) {
        
        @strongify(self);
        
        if ( [x isKindOfClass:[NSString class]]&&[x length]>0) {
            [BKHistoryViewModel colleageWithProv:x success:^(NSArray *dataArray) {
                self.zhiyuan3.colleageArray = dataArray;
                self.zhiyuan3.flag = 8004;
                
            } failure:^(NSString *message) {
                
            }];
        }
        
    }];
    
    [RACObserve(self.diqu2, text) subscribeNext:^(id x) {
        
        @strongify(self);
        
        if ( [x isKindOfClass:[NSString class]]&&[x length]>0) {
            [BKHistoryViewModel colleageWithProv:x success:^(NSArray *dataArray) {
                self.zhiyuan2.colleageArray = dataArray;
                self.zhiyuan2.flag = 8004;
                
            } failure:^(NSString *message) {
                
            }];
        }
        
    }];
    
    [RACObserve(self.diqu4, text) subscribeNext:^(id x) {
        
        @strongify(self);
        
        if ( [x isKindOfClass:[NSString class]]&&[x length]>0) {
            [BKHistoryViewModel colleageWithProv:x success:^(NSArray *dataArray) {
                self.zhiyuan4.colleageArray = dataArray;
                self.zhiyuan4.flag = 8004;
                
            } failure:^(NSString *message) {
                
            }];
        }
        
    }];
    
    [RACObserve(self.diqu5, text) subscribeNext:^(id x) {
        
        @strongify(self);
        
        if ( [x isKindOfClass:[NSString class]]&&[x length]>0) {
            [BKHistoryViewModel colleageWithProv:x success:^(NSArray *dataArray) {
                self.zhiyuan5.colleageArray = dataArray;
                self.zhiyuan5.flag = 8004;
                
            } failure:^(NSString *message) {
                
            }];
        }
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)p_submit:(id)sender {
    
    if ([_kaofen.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入考分" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
    
        [alertView show];
        return;
    }
    if ([_zhunkaozheng.text length]<9) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的准考证" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }
    
    if ([_kaoqu.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择考区" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }
    
    if ([_zhiyuan1.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入志愿一" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }

    if ([_zhiyuan2.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入志愿二" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }
    
    if ([_zhiyuan3.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入志愿三" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }
    if ([_zhiyuan4.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入志愿四" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }
    if ([_zhiyuan5.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入志愿五" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }

    __block id zhiyuan1;
    __block id zhiyuan2;
    __block id zhiyuan3;
    __block id zhiyuan4;
    __block id zhiyuan5;
    
    BKZhiYuanViewController *weakSelf = self;
    [_zhiyuan1.colleageArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj[@"name"] isEqualToString:weakSelf.zhiyuan1.text]) {
            zhiyuan1 = obj[@"cid"];
            *stop = YES;
        }
    }];
    
    [_zhiyuan5.colleageArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj[@"name"] isEqualToString:weakSelf.zhiyuan5.text]) {
            zhiyuan5 = obj[@"cid"];
            *stop = YES;
        }
    }];
    
    [_zhiyuan2.colleageArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj[@"name"] isEqualToString:weakSelf.zhiyuan2.text]) {
            zhiyuan2 = obj[@"cid"];
            *stop = YES;
        }
    }];
    
    [_zhiyuan3.colleageArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj[@"name"] isEqualToString:weakSelf.zhiyuan3.text]) {
            zhiyuan3 = obj[@"cid"];
            *stop = YES;
        }
    }];
    
    [_zhiyuan4.colleageArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj[@"name"] isEqualToString:weakSelf.zhiyuan4.text]) {
            zhiyuan4 = obj[@"cid"];
            *stop = YES;
        }
    }];
    

    [BKZhiYuanViewModel zhiyuanWithkq:_kaoqu.text kf:_kaofen.text zheng:_zhunkaozheng.text zy1:zhiyuan1 zy2:zhiyuan2 zy3:zhiyuan3 zy4:zhiyuan4 zy5:zhiyuan5 success:^(NSDictionary *dic) {
        BKResultZhiYuanViewController *result = [[BKResultZhiYuanViewController alloc] initWithNibName:NSStringFromClass([BKResultZhiYuanViewController class]) bundle:nil];
        result.data = dic;
        [self presentViewController:result animated:YES completion:nil];

    } failure:^(NSString *message) {
    
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];

        
    }];
    
}

@end
